package com.geralt.im.commands.impl;

import java.util.ArrayList;
import java.util.HashSet;

import com.geralt.im.command.Command;
import com.geralt.im.commandenum.Code;
import com.geralt.im.dao.impl.CustomerDaoImlp;
import com.geralt.im.model.Customer;
import com.geralt.im.model.Message;
import com.geralt.im.protocol.Response;
import com.geralt.im.server.IMServer;
import com.geralt.im.server.IMServerClient;

public class RegisterCommand extends Command {
	@Override
	public Response excute() {
		Customer customer = (Customer) parameter.get("REGISTER");
		// DAO
		String id = customer.getId();

		CustomerDaoImlp dao = new CustomerDaoImlp();
		Customer resultCus = dao.query(id);
		boolean validate = (resultCus == null) ;
		if(! IMServerClient.server.isRun())
			return getBadRequest("The server has some problem,please retry or feedback");
		receivers.clear();
		if (validate) {
			if (dao.persist(customer) == 1) {
				Message msg = new Message();
				msg.setContent("Register success");
				Response response = new Response();
				response.setStatusCode(Code.OK);
				response.setType("REGISTER");
				response.getParameter().put("MESSAGE", msg);
				return response;
			} else {
				return getBadRequest("The server has some problem,please retry or feedback");
			}
		}
		// return BAD_REQUEST
		return getBadRequest("User is exist, please change your user name");
	}
}
